package com.starchen.optimalselection.common.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.starchen.optimalselection.common.constant.ProductConstant;
import com.starchen.optimalselection.common.model.ResultPage;
import com.starchen.optimalselection.common.utils.PageUtils;
import com.starchen.optimalselection.common.mapper.AttrAndGroupRelationMapper;
import com.starchen.optimalselection.common.model.AttrAndGroupRelation;
import com.starchen.optimalselection.common.model.form.AttributeForm;
import com.starchen.optimalselection.common.model.vo.AttrQueryVo;
import com.starchen.optimalselection.common.model.vo.AttrRespVo;
import com.starchen.optimalselection.common.model.vo.AttributeVo;
import com.starchen.optimalselection.common.service.AttributeService;
import com.starchen.optimalselection.common.service.CategoryService;
import com.starchen.optimalselection.common.mapper.AttributeGroupMapper;
import com.starchen.optimalselection.common.mapper.AttributeMapper;
import com.starchen.optimalselection.common.mapper.CategoryMapper;
import com.starchen.optimalselection.common.model.Attribute;
import com.starchen.optimalselection.common.model.AttributeGroup;
import com.starchen.optimalselection.common.model.Category;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;


@Service
public class AttributeServiceImpl extends ServiceImpl<AttributeMapper, Attribute> implements AttributeService {

    @Autowired
    private AttributeMapper attrDao;

    @Autowired
    private AttrAndGroupRelationMapper relationMapper;

    @Autowired
    private AttributeGroupMapper attrGroupMaper;

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired(required = false)
    private CategoryService categoryService;

    @Override
    public ResultPage queryPage(AttributeForm attrQueryVo) {
        QueryWrapper<Attribute> queryWrapper = new QueryWrapper<Attribute>().eq("attr_type", attrQueryVo.getAttrType());

        String key = attrQueryVo.getKeyword();
        if(StringUtils.hasText(key)){
            queryWrapper.like("name",key);
        }

        IPage<Attribute> page = this.page(PageUtils.getPage(attrQueryVo.getPageIndex(),attrQueryVo.getPageSize()), queryWrapper);

        return new ResultPage(page);
    }

    public void saveAttribute(AttributeVo vo){
        Attribute attr = new Attribute();
        BeanUtils.copyProperties(vo,attr);
        //1、保存基本数据
        attrDao.insert(attr);
        //2、保存属性组关联关系,基本属性需保存，销售属性不用保存
        if(attr.getAttrType() == ProductConstant.AttrEnum.ATTR_TYPE_BASE.getCode()){
            AttrAndGroupRelation relationEntity = new AttrAndGroupRelation();
            relationEntity.setAttrGroupId(vo.getAttrGroupId());
            relationEntity.setAttrId(attr.getId());
            relationMapper.insert(relationEntity);
        }
    }

    @Override
    public List<Attribute> queryList(AttrQueryVo attrQueryVo) {
        QueryWrapper<Attribute> queryWrapper = new QueryWrapper<Attribute>().eq("attr_type", attrQueryVo.getAttrType());
        if(attrQueryVo.getCategoryId() != null){
            queryWrapper.eq("category_id",attrQueryVo.getCategoryId());
        }
        List<Attribute> attrList = attrDao.selectList(queryWrapper);

        return attrList;
    }

    @Override
    public void updateAttr(AttributeVo attr) {
        Attribute attrEntity = new Attribute();
        BeanUtils.copyProperties(attr,attrEntity);
        this.updateById(attrEntity);

        //1、修改分组关联
        if(attr.getAttrType() == ProductConstant.AttrEnum.ATTR_TYPE_BASE.getCode()){
            AttrAndGroupRelation relationEntity = new AttrAndGroupRelation();
            relationEntity.setAttrGroupId(attr.getAttrGroupId());
            relationEntity.setAttrId(attr.getId());

            Long count =  relationMapper.selectCount(new QueryWrapper<AttrAndGroupRelation>().eq("attr_id", attr.getId()));
            if(count>0){
                relationMapper.update(relationEntity,new UpdateWrapper<AttrAndGroupRelation>().eq("attr_id",attr.getId()));
            }else{
                relationMapper.insert(relationEntity);
            }
        }
    }

    @Override
    public AttrRespVo getAttr(Long attrId) {
        AttrRespVo respVo = new AttrRespVo();
        Attribute attrEntity = this.getById(attrId);
        BeanUtils.copyProperties(attrEntity,respVo);

        //1、设置分组信息
        if(attrEntity.getAttrType() == ProductConstant.AttrEnum.ATTR_TYPE_BASE.getCode()){
            AttrAndGroupRelation attrgroupRelation = relationMapper.selectOne(new QueryWrapper<AttrAndGroupRelation>().eq("attr_id", attrId));
            if(attrgroupRelation != null){
                respVo.setAttrGroupId(attrgroupRelation.getAttrGroupId());
                AttributeGroup attrGroup = attrGroupMaper.selectById(attrgroupRelation.getAttrGroupId());
                respVo.setGroupName(attrGroup.getName());
            }
        }

        //2、设置分类信息
        Long catelogId = attrEntity.getCategoryId();
        Long[] catelogPath = categoryService.getCategoryPath(catelogId);
        respVo.setCategoryPath(catelogPath);

        Category category = categoryMapper.selectById(catelogId);
        if(category !=null){
            respVo.setCategoryName(category.getName());
        }
        return respVo;
    }

    @Override
    public List<Attribute> getListByGroup(Long groupId) {
        List<AttrAndGroupRelation> entities = relationMapper.selectList(new QueryWrapper<AttrAndGroupRelation>().eq("attr_group_id", groupId));

        List<Long> attrIds = entities.stream().map((attr)->
                attr.getAttrId()).collect(Collectors.toList());

        if(attrIds == null || attrIds.size() == 0){
            return null;
        }
        List<Attribute> attrEntities = this.listByIds(attrIds);
        return attrEntities;
    }

    @Override
    public List<Attribute> selectAttrIds(List<Long> ids) {
        QueryWrapper<Attribute> wrapper = new QueryWrapper<>();
        wrapper.in("id", ids);
        wrapper.eq("search_type", 1);
        List<Attribute> list = this.list(wrapper);
        return list;
    }

}